---@class TBS.VirtualServer
local VirtualServer = TBS.CreateSingleton("VirtualServer")
local this = VirtualServer
local tempReportPath
function VirtualServer.RequestReport(oss,devOss,reportPath)
    local path = reportPath
    if path == nil then
        printError("战报有误reportPath：nil")
        return
    end
    if path == TBS.ReportMgr.reportPath then
        TBS.Battle.Restart("")
        return
    end
    tempReportPath = path
    if oss == 1 or devOss == 1 then
        dungeonHandler.ReqBattleReportPathMessage(0,path)
        NetMsgDecoder.RegisterResMessageBeforeCallback(144102,this.MessageCallback,nil,true)
    else
        local url = logicMgr.LoginManager.GetHttpServerUrl() .. "fight/"
        url = url .. tostring(path) .. ".bt"
        this.HttpGet(path,url)
    end
end
function VirtualServer.OnDestroy()
    NetMsgDecoder.UnRegisterResMessageBeforeCallback(144102,this.MessageCallback)
end
function VirtualServer.MessageCallback(msg)
    if tempReportPath == TBS.ReportMgr.reportPath then
        return
    end
    this.HttpGet(tempReportPath,msg.fullPath)
end
function VirtualServer.HttpGet(reportPath,url)
    log("组队战战报url==="..url)
    Http.Get(url, nil, function(success, cmdStr)
        if not success then
            logError("http get fight cmd failed. url: " .. url .. ". ErrMsg: " .. cmdStr)
            UISysTips.AddMsg("获取战报失败 "..reportPath)
            return
        end
        if reportPath == TBS.ReportMgr.reportPath then
            return
        end
        TBS.ReportMgr.reportPath = reportPath
        this.ResReport(cmdStr)
    end)
end
function VirtualServer.ResReport(cmd)
    TBS.Battle.Restart(cmd)
end

--region editor 相关
function VirtualServer.EditorPlayReport(str)
    if this.EditorIsPlaying() then
        TBS.RuleMgr.BattleFinished(TBS.Config.BattleResultType.bossDie)
        TBS.RuleMgr.Clear()
        TBS.ReportMgr.Clear()
    end
    utimer.delayCall(function()
        G.serverData = {roleInfo = {show = {playerId = 1}}}
        Config.serverTime = os.time({year = 2021,month = 8,day = 25,hour = 13}) * 1000
        InvasionHandler.teamData = {bossId = 1,bossLv = 1,failNum = 3,member = {}}
        if isOpenUI(PanelNames.UIInvasionBattle) then
            TBS.Battle.Restart(str)
        else
            showUI(PanelNames.UIInvasionBattle)
            TBS.Battle.Restart(str)
        end
    end,100)

end
function VirtualServer.EditorIsPlaying()
    return TBS.Battle.isStart
end
function VirtualServer.EditorGetReportCmd()
    local str = ""
    local _list = TBS.ReportMgr.initCmdList
    if _list then
        for k,v in pairs(_list) do
            str = str.."|"..v:ToString()
        end
    end
    local _list = TBS.ReportMgr.commandList
    if _list then
        for k,v in pairs(_list) do
            for k2,v2 in pairs(v) do
                str = str.."|"..v2:ToString()
            end
        end
    end
    return str
end
function VirtualServer.EditorGetCurTurn()
    return TBS.RuleMgr.turnCount or 0
end
function VirtualServer.EditorGetCmdState()
    local str = ""
    local _list = TBS.RuleMgr.executeCmdList
    if _list then
        for k,v in pairs(_list) do
            str = str.."|"..v.cid..","..tostring(v.isActive)
            if v.action == TBS.Config.FightActionType.ATTACK or v.action == TBS.Config.FightActionType.BUFF then
                str = str.." "..tostring(v.isEnd)
            else
                str = str.."      "
            end
        end
    end
    return str
end
function VirtualServer.EditorGotoRound(round)

end
--endregion editor 相关